package project1;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 五子棋主程序入口类
 *  <p>负责初始化游戏界面、组合功能组件、配置事件监听</p>
 * @see project1.DrawGomoku 棋盘绘制类
 * @see project1.SaveGomoku 保存游戏类
 * @see project1.SaveButtonListener 保存按钮事件监听类
 * @see project1.LoadButtonListener 加载按钮事件监听类
 * @see project1.UndoButtonListener 悔棋按钮事件监听类
 */
@Gomoku_Component("五子棋主程序入口类")
public class Gomoku {

    public static void main(String[] args) {
            /**
             * 初始化游戏界面
             * @param args 命令行参数（未使用）
             */
            JFrame frame = new JFrame("Gomoku");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setBounds(400,200,500,500);

            @Gomoku_Event(event = "创建主面板,使用BorderLayout布局")
            JPanel mainPanel = new JPanel();
            mainPanel.setLayout(new BorderLayout());


            @Gomoku_Event(event = "创建棋盘绘制类")
            DrawGomoku dg = new DrawGomoku();
            mainPanel.add(dg,BorderLayout.CENTER);

            @Gomoku_Event(event = "创建保存游戏类")
            SaveGomoku sg = new SaveGomoku();


            @Gomoku_Event(event = "创建按钮面板")
            JPanel buttonPanel = new JPanel();
            mainPanel.add(buttonPanel,BorderLayout.SOUTH);



            @Gomoku_Event(event = "模式一正常模式：玩家与玩家")
            JButton playerButton = new JButton("正常模式");
            playerButton.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                            if(dg.mouseAdapter2!=null){
                                    dg.removeMouseListener(dg.mouseAdapter2);
                            }
                            dg.Mode =1;
                            dg.addMouseListener(dg.mouseAdapter1);
                            dg.repaint();
                    }
            });
            buttonPanel.add(playerButton);

            @Gomoku_Event(event = "模式二人机对战：玩家与AI")
            JButton aiButton = new JButton("人机对战");
            aiButton.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                            if(dg.mouseAdapter1!=null){
                                    dg.removeMouseListener(dg.mouseAdapter1);
                            }
                            dg.Mode =2;
                            dg.addMouseListener(dg.mouseAdapter2);
                            dg.repaint();
                    }
            });
            buttonPanel.add(aiButton);



            @Gomoku_Event(event = "创建悔棋按钮")
            JButton undoButton = new JButton("悔棋");
            undoButton.addActionListener(new UndoButtonListener(dg));

            @Gomoku_Event(event = "创建保存按钮")
            //添加保存按钮
            JButton saveButton = new JButton("保存");
            saveButton.addActionListener(new SaveButtonListener(sg,dg));

            @Gomoku_Event(event = "创建加载按钮")
            JButton loadButton = new JButton("加载");
            loadButton.addActionListener(new LoadButtonListener(sg,dg));

            buttonPanel.add(undoButton);
            buttonPanel.add(saveButton);
            buttonPanel.add(loadButton);
            frame.add(buttonPanel);
            mainPanel.add(buttonPanel,BorderLayout.SOUTH);
            frame.add(mainPanel);
            frame.setVisible(true);
    }
}
